﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ZoneFiveSoftware.Common.Visuals;

namespace STCommon.View
{
    public class MaximizeActionBanner : ActionBanner
    {
        public MaximizeActionBanner()
        {
        }

        void maximizeButton_Click(object sender, EventArgs e)
        {
            if (MaximizeButtonClick != null)
            {
                MaximizeButtonClick(this, e);
            }

            m_PageToMaximize.PageMaximized = !m_PageToMaximize.PageMaximized;

            if (m_PageToMaximize.PageMaximized)
            {
                m_MaximizeButton.CenterImage = CommonResources.Images.View3PaneLowerLeft16;
            }
            else
            {
                m_MaximizeButton.CenterImage = CommonResources.Images.View2PaneLowerHalf16;
            }
        }

        public void SetMaximizable(IDetailPage pageToMaximize)
        {
            Debug.Assert(pageToMaximize != null);

            m_PageToMaximize = pageToMaximize;
            HasMaximizeButton = true;
        }

        [Browsable(false)]
        private bool HasMaximizeButton
        {
            get { return m_HasMaximizeButton; }
            set
            {
                if (HasMaximizeButton != value)
                {
                    m_HasMaximizeButton = value;

                    if (HasMaximizeButton && m_PageToMaximize != null)
                    {
                        Point location;

                        m_MaximizeButton = CreateMenuButton();
                        m_MaximizeButton.Size = new Size(23, 25);
                        m_MaximizeButton.Anchor = AnchorStyles.Top | AnchorStyles.Right;
                        m_MaximizeButton.CenterImage = CommonResources.Images.View2PaneLowerHalf16;

                        if (HasMenuButton)
                        {
                            if (RightToLeft == RightToLeft.Yes)
                            {
                                location = new Point(Controls[0].Location.X + m_MaximizeButton.Width + 2,
                                                     Controls[0].Location.Y);
                            }
                            else
                            {
                                location = new Point(Controls[0].Location.X - m_MaximizeButton.Width - 2,
                                                     Controls[0].Location.Y);
                            }
                        }
                        else
                        {
                            location = new Point(this.Width - m_MaximizeButton.Width - 2,
                                                 0);
                        }
                        m_MaximizeButton.Location = location;

                        m_MaximizeButton.Click += new EventHandler(maximizeButton_Click);
                        Controls.Add(m_MaximizeButton);
                    }
                    else if(m_MaximizeButton != null)
                    {
                        Controls.Remove(m_MaximizeButton);
                        m_MaximizeButton = null;
                        m_HasMaximizeButton = false;
                    }
                }
            }
        }

        public event EventHandler MaximizeButtonClick;

        private IDetailPage m_PageToMaximize = null;
        private ZoneFiveSoftware.Common.Visuals.Button m_MaximizeButton = null;
        private bool m_HasMaximizeButton = false;
    }
}
